<template>
<div class="message-board">
    <div class="section-title">留言板</div>
    <el-form :model="form" :rules="rules" ref="form" label-width="65px" label-position="left" class="message-form">
      <el-form-item label="留言" prop="content">
        <el-input type="textarea" :rows="5" v-model="form.content" placeholder="请输入内容"></el-input>
      </el-form-item>
      <el-form-item label="图片" prop="cpicture">
        <file-upload
          tip="点击上传图片"
          action="file/upload"
          :limit="1"
          :multiple="true"
          :fileUrls="form.cpicture?form.cpicture:''"
          @change="cpictureUploadChange"
        ></file-upload>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('form')">立即提交</el-button>
        <el-button @click="resetForm('form')">重置</el-button>
      </el-form-item>
    </el-form>

    <div class="message-list">
      <div v-for="item in infoList" :key="item.id" class="message-item">
        <div class="message-header">
          <el-avatar v-if="item.avatarurl" :size="50" :src="$config.baseUrl + item.avatarurl"></el-avatar>
          <el-avatar v-else :size="50" :src="require('@/assets/touxiang.png')"></el-avatar>
          <span class="username">用户：{{item.username}}</span>
        </div>
        <div class="message-content">
          {{item.content}}
        </div>
        <div v-if="item.cpicture" class="message-image">
          <img :src="$config.baseUrl+item.cpicture">
        </div>
        <div v-if="item.reply" class="reply-content">
          回复：{{item.reply}}
        </div>
        <div v-if="item.rpicture" class="reply-image">
          <img :src="$config.baseUrl+item.rpicture">
        </div>
      </div>
    </div>

    <el-pagination
      background
      class="pagination"
      :pager-count="7"
      :page-size="pageSize"
      :page-sizes="pageSizes"
      prev-text="<"
      next-text=">"
      :hide-on-single-page="true"
      :layout='["total","prev","pager","next","sizes","jumper"].join()'
      :total="total"
      @current-change="curChange"
      @prev-click="prevClick"
      @next-click="nextClick"
    ></el-pagination>

</div>
</template>

<script>
  export default {
    //数据集合
    data() {
      return {
		  layouts: '',
        form: {
          content: '',
          userid: localStorage.getItem('userid'),
          username: localStorage.getItem('username')
        },
        total: 1,
        pageSize: 20,
		pageSizes: [10,20,30,50],
        totalPage: 1,
        rules: {
          content: [
            { required: true, message: '请输入内容', trigger: 'blur' }
          ]
        },
        infoList: []
      }
    },
    created() {
      this.getInfo(1);
    },
    //方法集合
    methods: {
      getInfo(page) {
        this.$http.get('messages/list', {params: {page, limit: this.pageSize,sort:"addtime", order:"desc"}}).then(res => {
          if (res.data.code == 0) {
            this.infoList = res.data.data.list;
            this.total = res.data.data.total;
            this.pageSize = res.data.data.pageSize;this.pageSizes = [this.pageSize, this.pageSize*2, this.pageSize*3, this.pageSize*5];
            this.totalPage = res.data.data.totalPage;
          }
        });
      },
      curChange(page) {
        this.getInfo(page);
      },
      prevClick(page) {
        this.getInfo(page);
      },
      nextClick(page) {
        this.getInfo(page);
      },
      cpictureUploadChange(fileUrls) {
          this.form.cpicture = fileUrls;
      },
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            if(this.form.cpicture!=null) {
               this.form.cpicture = this.form.cpicture.replace(new RegExp(this.$config.baseUrl,"g"),"");
            }
            this.form.avatarurl = localStorage.getItem('headportrait')?localStorage.getItem('headportrait'):'';
            this.$http.post('messages/add', this.form).then(res => {
              if (res.data.code == 0) {
                this.$message({
                  type: 'success',
                  message: '留言成功!',
                  duration: 1500,
                  onClose: () => {
                    this.form.content = '';
                    this.getInfo(1);
                  }
                });
              }
            });
          } else {
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

<style lang="scss" scoped>
.message-board {
  width: 1200px;
  margin: 20px auto 0;
  padding: 30px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.1);
}

.section-title {
  font-size: 32px;
  font-weight: 600;
  text-align: center;
  color: #fff;
  background: linear-gradient(135deg, #e6a23c 0%, #f56c6c 100%);
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 30px;
}

.message-form {
  max-width: 800px;
  margin: 0 auto;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.message-list {
  margin-top: 40px;
}

.message-item {
  padding: 20px;
  margin-bottom: 20px;
  background: #f8f9fa;
  border-radius: 8px;
  transition: all 0.3s ease;

  &:hover {
    transform: translateY(-2px);
    box-shadow: 0 4px 12px rgba(0,0,0,0.1);
  }
}

.message-header {
  display: flex;
  align-items: center;
  margin-bottom: 15px;

  .username {
    margin-left: 15px;
    font-size: 16px;
    color: #606266;
  }
}

.message-content {
  padding: 15px;
  background: #fff;
  border-radius: 6px;
  color: #303133;
  line-height: 1.6;
}

.message-image, .reply-image {
  margin: 15px 0;
  
  img {
    max-width: 300px;
    max-height: 300px;
    border-radius: 6px;
    border: 2px solid #eee;
  }
}

.reply-content {
  margin-top: 20px;
  padding: 15px;
  background: #e1f3d8;
  border-radius: 6px;
  color: #67c23a;
}

.pagination {
  margin-top: 30px;
  text-align: center;

  /deep/ {
    .el-pagination__total,
    .el-pagination__sizes,
    .el-pagination__jump {
      margin-right: 16px;
    }

    .btn-prev,
    .btn-next,
    .number {
      background: #fff;
      border: 1px solid #dcdfe6;
      color: #606266;
      min-width: 32px;
      border-radius: 4px;
      margin: 0 4px;

      &:hover {
        color: #e6a23c;
        border-color: #e6a23c;
      }

      &.active {
        background: #e6a23c;
        border-color: #e6a23c;
        color: #fff;
      }
    }
  }
}
</style>
